using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows;
using System.Windows.Forms;
using Common;
using Common.Enum;
using SExperiment;
using SExperiment.ExperimentResult;
using SWellPlate;

namespace MainView.MainControls.Chart
{
	public class ADCurveChart : BaseADCurveChart
	{
		public static readonly DependencyProperty StandardAxixProperty = DependencyProperty.Register("StandardAxix", typeof(bool), typeof(ADCurveChart), new UIPropertyMetadata(true));

		public static readonly DependencyProperty AxixReverseProperty = DependencyProperty.Register("AxixReverse", typeof(bool), typeof(ADCurveChart), new UIPropertyMetadata(false));

		public static readonly DependencyProperty DisplayBackgroudProperty = DependencyProperty.Register("DisplayBackgroud", typeof(bool), typeof(ADCurveChart), new UIPropertyMetadata(true));

		public bool StandardAxix
		{
			get
			{
				return (bool)GetValue(StandardAxixProperty);
			}
			set
			{
				SetValue(StandardAxixProperty, value);
			}
		}

		public bool AxixReverse
		{
			get
			{
				return (bool)GetValue(AxixReverseProperty);
			}
			set
			{
				SetValue(AxixReverseProperty, value);
			}
		}

		public bool DisplayBackgroud
		{
			get
			{
				return (bool)GetValue(DisplayBackgroudProperty);
			}
			set
			{
				SetValue(DisplayBackgroudProperty, value);
			}
		}

		public ADCurveChart()
		{
			base.Margin = new Thickness(1.0);
			base.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
			base.VerticalAlignment = VerticalAlignment.Stretch;
			SetResourceReference(BaseCurveChart.CaptionProperty, "allele_genes_curve");
			base.MinX = 0.0;
			base.MaxX = 1.0;
			base.MinY = 0.0;
			base.MaxY = 1.0;
			base.TitleXFormat = "0.000";
			base.TitleYFormat = "0.000";
			base.UltraChart.SetAxixXToIntTickMark(false);
		}

		public override string GetToolTipFormatX()
		{
			return "0.000";
		}

		public override string GetToolTipFormatY()
		{
			return "0.000";
		}

		protected override void SetMaxX(double value)
		{
		}

		protected override void SetMinX(double value)
		{
		}

		protected override void SetMaxY(double value)
		{
		}

		protected override void SetMinY(double value)
		{
		}

		protected override void RefreshAxisTitle()
		{
			if (base.Targets == null || base.Targets.Count <= 0 || base.Targets[0].WellCell == null || base.Targets[0].WellCell.Well == null || base.Targets[0].WellCell.Well.Sample == null || base.Targets[0].WellCell.Well.Sample.Items.Count != 2)
			{
				return;
			}
			string targetName = base.Targets[0].WellCell.Well.Sample.Items[0].TargetName;
			string targetName2 = base.Targets[0].WellCell.Well.Sample.Items[1].TargetName;
			base.TitleX = (AxixReverse ? targetName2 : targetName);
			base.TitleY = (AxixReverse ? targetName : targetName2);
			List<List<Dot>> list = null;
			list = ((!StandardAxix) ? (from s in base.Targets
				where ((ADWellResult)s.WellCell.Well.Result).PolarCurve.CurvePoint.Count > 0
				select ((ADWellResult)s.WellCell.Well.Result).PolarCurve.CurvePoint).ToList() : (from s in base.Targets
				where ((ADWellResult)s.WellCell.Well.Result).Curve.CurvePoint.Count > 0
				select ((ADWellResult)s.WellCell.Well.Result).Curve.CurvePoint).ToList());
			if (list.Count <= 0 || list.Sum((List<Dot> s) => s.Count) <= 0)
			{
				return;
			}
			double num = list.Select((List<Dot> s) => s.Min((Dot s1) => s1.X)).Min();
			double num2 = list.Select((List<Dot> s) => s.Min((Dot s1) => s1.X)).Max();
			double num3 = list.Select((List<Dot> s) => s.Min((Dot s1) => s1.Y)).Min();
			double num4 = list.Select((List<Dot> s) => s.Min((Dot s1) => s1.Y)).Max();
			double num5 = num - (num2 - num) * 0.1;
			double num6 = num2 + (num2 - num) * 0.1;
			double num7 = num3 - (num4 - num3) * 0.1;
			double num8 = num4 + (num4 - num3) * 0.1;
			base.UltraChart.MinX = (AxixReverse ? num7 : num5);
			base.UltraChart.MaxX = (AxixReverse ? num8 : num6);
			base.UltraChart.MinY = (AxixReverse ? num5 : num7);
			base.UltraChart.MaxY = (AxixReverse ? num6 : num8);
		}

		protected override bool GetSeriesVisible(ChartData chartData)
		{
			if (!DisplayBackgroud)
			{
				return chartData.WellCell.IsSelected;
			}
			return true;
		}

		protected override Color GetChartDataSeriesColor(ChartData chartData)
		{
			if (!chartData.WellCell.IsSelected)
			{
				return Utility.GetDrawingColor("GenoADBackgroud");
			}
			return base.GetChartDataSeriesColor(chartData);
		}

		protected override ESeriesType GetSymbolIcon(ChartData chartData)
		{
			switch (((ADWellResult)chartData.WellCell.Well.Result).TypeNo)
			{
			case EADGenoType.GenoNegative:
				return ESeriesType.Square;
			case EADGenoType.GenoUnknown:
				return ESeriesType.X;
			default:
				return ESeriesType.Circle;
			}
		}

		public override Curve GetCurve(SampleTargetItem item)
		{
			ChartData chartData = base.Targets.Where((ChartData s) => s.SampleTargetItem == item).FirstOrDefault();
			Curve curve = null;
			curve = ((!StandardAxix) ? ((ADWellResult)chartData.WellCell.Well.Result).PolarCurve : ((ADWellResult)chartData.WellCell.Well.Result).Curve);
			Curve curve2;
			if (!AxixReverse)
			{
				curve2 = curve;
			}
			else
			{
				curve2 = new Curve();
				foreach (Dot item2 in curve.CurvePoint)
				{
					curve2.CurvePoint.Add(new Dot
					{
						X = item2.Y,
						Y = item2.X
					});
				}
			}
			return curve2;
		}

		public override void RefreshSeries(bool refreshData = false)
		{
			ClearAllChart();
			base.RefreshSeries(refreshData);
			RefreshChartSeriesColor();
		}

		protected override void InitMenu()
		{
			base.UltraChart.ContentMenu.Opening += ContentMenu_Opening;
			base.UltraChart.ContentMenu.Items.Add(new ToolStripSeparator());
			ToolStripMenuItem toolStripMenuItem = new ToolStripMenuItem();
			toolStripMenuItem.Text = Utility.GetMessage("cartesian_coordinates");
			toolStripMenuItem.Click += itemLinearAxis_Click;
			base.UltraChart.ContentMenu.Items.Add(toolStripMenuItem);
			toolStripMenuItem = new ToolStripMenuItem();
			toolStripMenuItem.Text = Utility.GetMessage("polar_axis");
			toolStripMenuItem.Click += itemPolarAxis_Click;
			base.UltraChart.ContentMenu.Items.Add(toolStripMenuItem);
			toolStripMenuItem = new ToolStripMenuItem();
			toolStripMenuItem.Text = Utility.GetMessage("axix_reverse");
			toolStripMenuItem.Click += itemAxixReverse_Click;
			base.UltraChart.ContentMenu.Items.Add(toolStripMenuItem);
			toolStripMenuItem = new ToolStripMenuItem();
			toolStripMenuItem.Text = Utility.GetMessage("axis_displaybackgroud");
			toolStripMenuItem.Click += itemAxixDisplayBackgroud_Click;
			base.UltraChart.ContentMenu.Items.Add(toolStripMenuItem);
		}

		private void itemLinearAxis_Click(object sender, EventArgs e)
		{
			if (!StandardAxix)
			{
				StandardAxix = true;
				base.UltraChart.IsCursomAxisValue = false;
				RefreshSeries(true);
				base.UltraChart.RefreshAxisXInterval();
				base.UltraChart.RefreshAxisYInterval();
			}
		}

		private void itemPolarAxis_Click(object sender, EventArgs e)
		{
			if (StandardAxix)
			{
				StandardAxix = false;
				base.UltraChart.IsCursomAxisValue = false;
				AxixReverse = false;
				RefreshSeries(true);
				base.UltraChart.RefreshAxisXInterval();
				base.UltraChart.RefreshAxisYInterval();
			}
		}

		private void itemAxixReverse_Click(object sender, EventArgs e)
		{
			AxixReverse = !AxixReverse;
			base.UltraChart.IsCursomAxisValue = false;
			RefreshSeries(true);
			base.UltraChart.RefreshAxisXInterval();
			base.UltraChart.RefreshAxisYInterval();
		}

		private void itemAxixDisplayBackgroud_Click(object sender, EventArgs e)
		{
			DisplayBackgroud = !DisplayBackgroud;
			RefreshSeries();
		}

		protected void ContentMenu_Opening(object sender, CancelEventArgs e)
		{
			foreach (ToolStripItem item in base.UltraChart.ContentMenu.Items)
			{
				if (item is ToolStripMenuItem)
				{
					ToolStripMenuItem toolStripMenuItem = (ToolStripMenuItem)item;
					if (toolStripMenuItem.Text == Utility.GetMessage("cartesian_coordinates"))
					{
						toolStripMenuItem.Checked = StandardAxix;
					}
					else if (toolStripMenuItem.Text == Utility.GetMessage("polar_axis"))
					{
						toolStripMenuItem.Checked = !StandardAxix;
					}
					else if (toolStripMenuItem.Text == Utility.GetMessage("axix_reverse"))
					{
						toolStripMenuItem.Checked = AxixReverse;
						toolStripMenuItem.Enabled = StandardAxix;
					}
					else if (toolStripMenuItem.Text == Utility.GetMessage("axis_displaybackgroud"))
					{
						toolStripMenuItem.Checked = DisplayBackgroud;
					}
				}
			}
		}
	}
}
